/*
 * jlb
 */
package com.jlb.user.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.UserGroupException;
import com.jlb.setting.dao.SettingDAO;
import com.jlb.user.dao.UserGroupDAO;
import com.jlb.user.entity.UserGroup;
import com.jlb.user.entity.UserGroupState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userGroupSV")
@Slf4j
public class UserGroupSVImpl extends BaseSVImpl<UserGroup, Long> implements UserGroupSV {

    @Autowired
    private UserGroupDAO userGroupDAO;

    @Autowired
    private SettingDAO settingDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return userGroupDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(UserGroup entity) throws BaseException {
        entity.setCreateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象UserGroup
     *
     * @param id * @param userCode 用户编码* @param preUserCode 上级编码
     * @return UserGroup
     */
    @Override
    public UserGroup load(java.lang.Long id, java.lang.String userCode, java.lang.String preUserCode) {
        if (id == null && userCode == null && preUserCode == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        param.put("preUserCode", preUserCode);
        return userGroupDAO.load(param);
    }

    /**
     * 加载一个对象UserGroup 通过id
     *
     * @param id
     * @return UserGroup
     */
    @Override
    public UserGroup loadById(java.lang.Long id) {
        if (id == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return userGroupDAO.loadById(id);
    }

    /**
     * 加载一个对象UserGroup 通过userCode
     *
     * @param userCode 用户编码
     * @return UserGroup
     */
    @Override
    public UserGroup loadByUserCode(java.lang.String userCode) {
        if (userCode == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return userGroupDAO.loadByUserCode(userCode);
    }

    /**
     * 加载一个对象UserGroup 通过preUserCode
     *
     * @param preUserCode 上级编码
     * @return UserGroup
     */
    @Override
    public UserGroup loadByPreUserCode(java.lang.String preUserCode) {
        if (preUserCode == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return userGroupDAO.loadByPreUserCode(preUserCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 UserGroup 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, UserGroupState newState, UserGroupState... oldStates) {
        if (id == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userGroupDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键userCode,oldStates 共同更新 UserGroup 的状态到newState状态
     *
     * @param userCode  用户编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByUserCode(java.lang.String userCode, UserGroupState newState, UserGroupState... oldStates) {
        if (userCode == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userGroupDAO.updateStateByUserCode(userCode, new Date(), newState, oldStates);
    }

    /**
     * 根据主键preUserCode,oldStates 共同更新 UserGroup 的状态到newState状态
     *
     * @param preUserCode 上级编码
     * @param newState    新状态
     * @param oldStates   旧状态集合
     */
    @Override
    public void updateStateByPreUserCode(java.lang.String preUserCode, UserGroupState newState, UserGroupState... oldStates) {
        if (preUserCode == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userGroupDAO.updateStateByPreUserCode(preUserCode, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 UserGroup 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, UserGroupState state) {
        if (id == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userGroupDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键userCode 更新 UserGroup 的状态到另一个状态
     *
     * @param userCode 用户编码
     * @param state    状态
     */
    @Override
    public void updateByUserCode(java.lang.String userCode, UserGroupState state) {
        if (userCode == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userGroupDAO.updateByUserCode(userCode, state, new Date());
    }

    /**
     * 根据主键preUserCode 更新 UserGroup 的状态到另一个状态
     *
     * @param preUserCode 上级编码
     * @param state       状态
     */
    @Override
    public void updateByPreUserCode(java.lang.String preUserCode, UserGroupState state) {
        if (preUserCode == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userGroupDAO.updateByPreUserCode(preUserCode, state, new Date());
    }


    /**
     * 删除对象UserGroup
     *
     * @param id * @param userCode 用户编码* @param preUserCode 上级编码
     * @return UserGroup
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String userCode, java.lang.String preUserCode) {
        if (id == null && userCode == null && preUserCode == null) {
            throw new UserGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        param.put("preUserCode", preUserCode);
        userGroupDAO.delete(param);
    }


    /**
     * 查询UserGroup分页
     *
     * @param userGroup 对象
     * @param offset    查询开始行
     * @param limit     查询行数
     * @return List<UserGroup>
     */
    @Override
    public List<UserGroup> list(UserGroup userGroup, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (userGroup != null) {
            map = JSON.parseObject(JSON.toJSONString(userGroup, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return userGroupDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(UserGroup userGroup) {
        Map<String, Object> map = null;
        if (userGroup != null) {
            map = JSON.parseObject(JSON.toJSONString(userGroup, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return userGroupDAO.count(map);
    }

    /**
     * 查询UserGroup分页
     *
     * @param id     * @param userCode  用户编码* @param preUserCode  上级编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<UserGroup>
     */
    @Override
    public List<UserGroup> list(java.lang.Long id, java.lang.String userCode, java.lang.String preUserCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        param.put("preUserCode", preUserCode);
        return userGroupDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String userCode, java.lang.String preUserCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        param.put("preUserCode", preUserCode);
        return userGroupDAO.count(param);
    }

    /**
     * 查询UserGroup列表
     *
     * @param preUserCode 上级用户编码
     * @return List<UserGroup>
     */
    @Override
    public List<UserGroup> listMyGroup(@Param("preUserCode") String preUserCode) {
        return userGroupDAO.listMyGroup(preUserCode);
    }

}
